`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Module: AUDIO_TUNE                                                           //
// Author: Zhiyuan Lin                                                          //
// Date: 3/30/2014                                                              //
// Description: This module describe the tune.                                  //
// Detail: Currently it is Chinese melody "Liang Zhu"                           //
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
// UPDATE: Currently 34s of playback  4/2/2014                                  //
//////////////////////////////////////////////////////////////////////////////////
module AUDIO_TUNE(speaker, clk_div, clk_4Hz, tone);
	 output speaker;
	 output reg [4:0] tone;
	 output reg clk_4Hz;
	 input  clk_div;
	 reg  [23:0] counter_4Hz, counter_6MHz;
	 reg  [13:0] count, origin;
	 reg  clk_6MHz;
    reg  [7:0]  playback_length; 
	 reg speaker_temp;

assign speaker = (origin != 0)? speaker_temp : 1'b1;
  
always @(posedge clk_div) begin   
  if(counter_6MHz == 4) begin
    counter_6MHz = 0;
    clk_6MHz = ~clk_6MHz;
  end
  else begin
    counter_6MHz = counter_6MHz + 24'b1;
  end
end

always @(posedge clk_div) begin       
  if(counter_4Hz == 3125000) begin	
    counter_4Hz = 0;
    clk_4Hz = ~clk_4Hz;
  end
  else begin
    counter_4Hz = counter_4Hz + 24'b1;
  end
end

always @(posedge clk_6MHz) begin
  if(count == 16383) begin //16383 = 11111111111111
    count = origin;
    speaker_temp = ~speaker_temp;
  end
  else begin
    count = count + 14'b1;
  end
end

always @(posedge clk_4Hz) begin
  case(tone)
    'd0 : origin = 'd0;
    'd1 : origin = 'd4916; //low1 (middle C)
	 'd2 : origin = 'd5560; //low1#
    'd3 : origin = 'd6168; //low2
	 'd4 : origin = 'd6740; //low2#
    'd5 : origin = 'd7281; //low3
    'd6 : origin = 'd7791; //low4
	 'd7 : origin = 'd8275; //low4#
    'd8 : origin = 'd8730; //low5
	 'd9 : origin = 'd9159; //low5#
    'd10 : origin = 'd9565; //low6
	 'd11 : origin = 'd9948; //low6#
    'd12 : origin = 'd10310; //low7
    'd13 : origin = 'd10647; //middle1
	 'd14 : origin = 'd10972; //middle1#
    'd15 : origin = 'd11272; //middle2
	 'd16 : origin = 'd11562; //middle2#
    'd17 : origin = 'd11831; //middle3
    'd18 : origin = 'd12087; //middle4
	 'd19 : origin = 'd12329; //middle4#
    'd20 : origin = 'd12556; //middle5
	 'd21 : origin = 'd12771; //middle5#
    'd22 : origin = 'd12974; //middle6
	 'd23 : origin = 'd13165; //middle6# 
    'd24 : origin = 'd13346; //middle7
    'd25 : origin = 'd13516; //high1
	 'd26 : origin = 'd13677; //high1#	 
    'd27 : origin = 'd13829; //high2
	 'd28 : origin = 'd13972; //high2#
    'd29 : origin = 'd14108; //high3
    'd30 : origin = 'd14235; //high4
	 'd31 : origin = 'd14356; //high4#
    'd32 : origin = 'd14470; //high5
	 'd33 : origin = 'd14577; //high5#
    'd34 : origin = 'd14678; //high6
	 'd35 : origin = 'd14774; //high6#
    'd36 : origin = 'd14864; //high7
    default : origin = 'd0;
  endcase             
end

always @(posedge clk_4Hz) begin
  if(playback_length == 138)
    playback_length = 0;
  else
    playback_length = playback_length + 8'b1;
  case(playback_length)
  0: tone = 5; //low3, 4clock
  1: tone = 5; 
  2: tone = 5;
  3: tone = 5;
  4: tone = 8; //low5, 3clock
  5: tone = 8; 
  6: tone = 8;
  7: tone = 10; //low6
  8: tone = 13; //middle1, 3clock
  9: tone = 13;
  10: tone = 13;
  11: tone = 15; //middle2
  12: tone = 10; //low6
  13: tone = 13; //middle1
  14: tone = 8; //low5, 2clock
  15: tone = 8; 
  16: tone = 20; //middle5, 3clock
  17: tone = 20; 
  18: tone = 20;
  19: tone = 25; //high1
  20: tone = 22; //middle6
  21: tone = 20; //middle5
  22: tone = 17; //middle3
  23: tone = 20; //middle5
  24: tone = 15; //middle2, 11clock
  25: tone = 15; 
  26: tone = 15;
  27: tone = 15;
  28: tone = 15;
  29: tone = 15;
  30: tone = 15;
  31: tone = 0;
  32: tone = 15;
  33: tone = 15;
  34: tone = 15;
  35: tone = 17; //middle3
  36: tone = 12; //low7, 2clock
  37: tone = 12;
  38: tone = 10; //low6, 2clock
  39: tone = 10;
  40: tone = 8; //low5, 3clock
  41: tone = 8;
  42: tone = 8;
  43: tone = 10; //low6
  44: tone = 13; //middle1, 2clock
  45: tone = 13;
  46: tone = 15; //middle2, 2clock
  47: tone = 15;
  48: tone = 5; //low3, 2clock
  49: tone = 5;
  50: tone = 13; //middle1, 2clock
  51: tone = 13;
  52: tone = 10; //low6
  53: tone = 8; //low5
  54: tone = 10; //low6
  55: tone = 13; //middle1
  56: tone = 8; //low5, 8clock
  57: tone = 8; 
  58: tone = 8;
  59: tone = 8;
  60: tone = 8;
  61: tone = 8;
  62: tone = 8;
  63: tone = 8;
  64: tone = 17; //middle3, 3clock
  65: tone = 17;
  66: tone = 17;
  67: tone = 20; //middle5
  68: tone = 12; //low7, 2clock
  69: tone = 12;
  70: tone = 15; //middle2, 2clock
  71: tone = 15;
  72: tone = 10; //low6
  73: tone = 13; //middle1
  74: tone = 8; //low5, 8clock
  75: tone = 8;
  76: tone = 8;
  77: tone = 8;
  78: tone = 8;
  79: tone = 8;
  80: tone = 8;
  81: tone = 8;
  82: tone = 5; //low3, 2clock
  83: tone = 5;
  84: tone = 8; //low5
  85: tone = 5; //low3, 2clock
  86: tone = 5;
  87: tone = 8; //low5
  88: tone = 10; //low6
  89: tone = 12; //low7
  90: tone = 15; //middle2
  91: tone = 10; //low6, 6clock
  92: tone = 10;
  93: tone = 10;
  94: tone = 10;
  95: tone = 10;
  96: tone = 10;
  97: tone = 8; //low5
  98: tone = 10; //low6
  99: tone = 13; //middle1, 3clock
  100: tone = 13;
  101: tone = 13;
  102: tone = 15; //middle2
  103: tone = 20; //middle5, 2clock
  104: tone = 20;
  105: tone = 17; //middle3, 2clock
  106: tone = 17;
  107: tone = 15; //middle2, 2clock
  108: tone = 15; 
  109: tone = 17; //middle3
  110: tone = 15; //middle2
  111: tone = 13; //middle1, 2clock
  112: tone = 13;
  113: tone = 10; //low6
  114: tone = 8; //low5
  115: tone = 5; //low3, 4clock
  116: tone = 5;
  117: tone = 5;
  118: tone = 5;
  119: tone = 13; //middle1, 4clock
  120: tone = 13;
  121: tone = 13;
  122: tone = 13;
  123: tone = 10; //low6, 2clock
  124: tone = 10;
  125: tone = 13; //middle1
  126: tone = 10; //low6
  127: tone = 8; //low5
  128: tone = 5; //low3
  129: tone = 8; //low5
  130: tone = 10; //low6
  131: tone = 13; //middle1
  132: tone = 8; //low5, 6clock
  133: tone = 8;
  134: tone = 8;
  135: tone = 8;
  136: tone = 8;
  137: tone = 8; 
  default: tone = 0;
  endcase
end
endmodule